package com.ruike.alisurface.services;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.ruike.alisurface.utils.APPUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

/**
 * 页面超时广播重启类
 */
public class TimeOutBroadcastReceiver extends BroadcastReceiver {
    int mNumber_of_intervals = 0;//间隔次数  60 次即为1小时

    /**
     * 注册
     */
    public void initTimePrompt(Context mContext) {
        L.e("注册页面长时间监听="+mContext.getClass().getSimpleName());
        IntentFilter timeFilter = new IntentFilter();
        timeFilter.addAction(Intent.ACTION_TIME_TICK);
        mContext.registerReceiver(this, timeFilter);
    }

    /**
     * 取消
     */
    public void unitTimePrompt(Context mContext) {
        L.e("解除页面长时间监听="+mContext.getClass().getSimpleName());
        mContext.unregisterReceiver(this);
    }

    /**
     *
     * 重启次数及网络异常页状态
     */
    public   void infoRest() {
        ShareUtils.getInstance().putInt(ShareKey.IS_OFFLINE_RESTART_APPCOUNT, 10);
        ShareUtils.getInstance().putBoolean(ShareKey.IS_NETWORK_STATUS, false);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        mNumber_of_intervals++;
        if (mNumber_of_intervals >= 10) {
            L.e("长时间停留在=" + context.getClass().getSimpleName() + "页  开始重启");
            infoRest();
            APPUtils.RestBoot();
        }
    }
}
